package com.ruima.study.arithmetic.linkednode;

/**
 * <p>
 * <p></p>
 * <p>
 * <p>Copyright (c) 2024-09-22 10:30 , 马锐个人 All Rights Reserve</p>
 *
 * @author marui
 * @version [V1.0]
 */
public class ReverseLinkedList {
    public static void main(String[] args) {
        ListNode node5 = new ListNode(5,null);
        ListNode node4 = new ListNode(4,node5);
        ListNode node3 = new ListNode(3,node4);
        ListNode node2 = new ListNode(2,node3);
        ListNode node1 = new ListNode(1,node2);

        ListNode node = iterate(node1);
        System.out.println(node);
    }

    public static ListNode iterate(ListNode head) {
        ListNode prev = null;
        ListNode curr;
        ListNode next;

        curr = head;
        while (curr != null) {
            next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }


}

class ListNode {

    int val;
    ListNode next;

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

}


